/**
 * ManageServiceImpl.java 2015年12月18日 下午4:05:38
 *
 * Copyright (c) 2010-2061 leying.com, Inc. All rights reserved.
 *
 * @Description TODO
 * @version 3.0
 *
 */
package com.xiake.service.impl.manager;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiake.core.entity.manager.Menu;
import com.xiake.core.entity.manager.MngUser;
import com.xiake.core.entity.manager.Role;
import com.xiake.dao.manager.MenuDao;
import com.xiake.dao.manager.MngUserDao;
import com.xiake.dao.manager.RoleDao;
import com.xiake.service.manager.ManageService;
import com.xiake.utils.MD5Util;
import com.xiake.utils.PageInfo;


@Service
public class ManageServiceImpl implements ManageService {

  @Autowired
  private MngUserDao mngUserDao;
  @Autowired
  private MenuDao menuDao;
  @Autowired
  private RoleDao roleDao;

  @Override
  public MngUser login(String username, String password) throws Exception {
    password = MD5Util.MD5(password);
    Map<String, Object> sqlMap = new HashMap<String, Object>();
    sqlMap.put("username", username);
    sqlMap.put("password", password);
    MngUser muser = mngUserDao.findForLogin(sqlMap);
    return muser;
  }

  @Override
  public Serializable saveMngUser(String truename, String username, String password, Integer roleId,
      Integer id) throws Exception {
	if(!StringUtils.isEmpty(password)){
		password = MD5Util.MD5(password);
	}
    MngUser muser = new MngUser(truename, username, password, roleId);
    if (id == null || id.intValue() == 0) {
      //mngUserDao.insert(muser);
      mngUserDao.save(muser);
      id = muser.getId();
    } else {
      muser.setId(id);
      if(StringUtils.isEmpty(password)){
    	  muser.setPassword(null);
      }
      //mngUserDao.updateById(muser);
      mngUserDao.save(muser);
    }
    return id;
  }

  @Override
  public List<MngUser> findMngUserList(Integer roleId, String name, PageInfo pageInfo) throws Exception {
    Map<String, Object> sqlMap = new HashMap<String, Object>();
    sqlMap.put("roleId", roleId);
    if(!StringUtils.isEmpty(name)){
    	sqlMap.put("name", name);
    }
    sqlMap.put("pageInfo", pageInfo);
    List<MngUser> musers = mngUserDao.query(sqlMap);
    
    int count = musers.size();
    if(pageInfo != null && musers.size() == pageInfo.getPerPageNum()){
      count = mngUserDao.count(sqlMap);
    }
    pageInfo.setTotalNum(count);
    pageInfo.calculateAllPage();
    return musers;
  }

	@Override
	public List<Menu> findMenuByRoleId(Integer roleId) throws Exception {
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("roleId", roleId);
		List<Menu> menus = menuDao.query(sqlMap);
		this.findLeaf(menus, roleId,false);
		return menus;
	}
	
	@Override
	public List<Menu> findMenuByWithChecked(Integer roleId) throws Exception {
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("roleId", roleId);
		List<Menu> menus = menuDao.findListWithChecked(sqlMap);
		this.findLeaf(menus, roleId,true);
		return menus;
	}
	
	private void findLeaf(List<Menu> menus,Integer roleId,boolean withChecked) throws Exception{
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("roleId", roleId);
		if(menus != null && menus.size() > 0){
			for(Menu menu : menus){
				if(menu != null && menu.getIsLeaf() != null && menu.getIsLeaf().intValue() == 0){
					sqlMap.put("parentId", menu.getId());
					List<Menu> cmenus = null;
					if(withChecked){
						cmenus = menuDao.findListWithChecked(sqlMap);
					} else{
						cmenus = menuDao.query(sqlMap);
					}
					menu.setMenus(cmenus);
					this.findLeaf(cmenus, roleId,withChecked);
				}
			}
		}
	}

	@Override
	public List<Role> findRoleList() throws Exception {
		
		return roleDao.query(null);
	}

	@Override
	public MngUser findMngUserById(Integer mngUserId) throws Exception {
		
		return mngUserDao.get(mngUserId);
	}

	@Override
	public boolean checkUsername(String username) throws Exception {
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("username", username);
		int count = mngUserDao.count(sqlMap);
		if(count == 0){
			return true;
		}
		return false;
	}

	@Override
	public void saveMenu(Integer id, String name, String url, String icon,
			Integer parentId, Integer index) throws Exception {
		Menu menu = new Menu();
		menu.setId(id);
		menu.setIcon(icon);
		menu.setIsLeaf(StringUtils.isEmpty(url)?0:1);
		menu.setName(name);
		menu.setParentId(parentId);
		menu.setIndex(index);
		menu.setUrl(url);
		if (id == null || id.intValue() == 0) {
			menuDao.save(menu);
		} else{
			menuDao.save(menu);
		}
	}

	@Override
	public Role findRoleById(Integer roleId) throws Exception {
		return roleDao.get(roleId);
	}

	@Override
	public void saveRole(Integer id, String name, Integer[] menuids,Integer createrId) throws Exception {
		Role role = new Role();
		role.setId(id);
		role.setName(name);
		role.setCreaterId(createrId);
		if(id == null || id.intValue() == 0){
			roleDao.save(role);
		} else{
			roleDao.save(role);
			roleDao.removeMenuByRoleId(id);
		}
		if(menuids != null && menuids.length > 0){
			for(Integer menuId : menuids){
				if(menuId != null){
					roleDao.addMenuByRoleId(menuId, role.getId(), createrId);
				}
			}
		}
		
	}

	@Override
	public void deleteMUserById(Integer muserId) throws Exception {
		mngUserDao.deleteById(muserId);
		
	}

	@Override
	public void deleteMenuById(Integer menuId) throws Exception {
		menuDao.deleteById(menuId);
		
	}

	@Override
	public void deleteRoleById(Integer roleId) throws Exception {
		roleDao.deleteById(roleId);
		
	}

}
